﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class ArticleCateGoryService : IArticleCateGoryService
    {
        private IArticleCateGoryManager _acg;
        public ArticleCateGoryService(IArticleCateGoryManager acg)
        {
            _acg = acg;
        }
        public async Task<int> AddArticleCateGoryAsync(string title)
        {
            return await _acg.AddAsync(new Model.ArticleCateGory
            {
                Title = title,
            });
        }

        public async Task<int> DeleteArticleCateGoryAsync(int id)
        {
            var data = await _acg.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _acg.DeleteAsync(data);
        }

        public async Task<List<ArticleCateGoryDto>> GetAllArticleCateGoryAsync(int deleteId = 1)
        {
            var data = await _acg.GetChooseAsync(ac => ac.DeleteId == deleteId);
            return data == null ? new List<ArticleCateGoryDto>() : data.Select(ac => new ArticleCateGoryDto
            {
                Id = ac.Id,
                Title = ac.Title,
                CreateTime = ac.CreateTime,
                UpdateTime = ac.UpdateTime
            }).ToList();
        }

        public async Task<ArticleCateGoryDto> GetByIdAsync(int id)
        {
            var data = await _acg.GetById(id);
            return data == null ? new ArticleCateGoryDto() : new ArticleCateGoryDto
            {
                Id = data.Id,
                Title = data.Title,
                UpdateTime = data.UpdateTime,
                CreateTime = data.CreateTime
            };
        }

        public async Task<List<ArticleCateGoryDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _acg.GetChooseAsync(ac => ac.Title.Contains(title) && ac.DeleteId == deleteId);
            return data == null ? new List<ArticleCateGoryDto>() : data.Select(ac => new ArticleCateGoryDto
            {
                Id = ac.Id,
                Title = ac.Title,
                UpdateTime = ac.UpdateTime,
                CreateTime = ac.CreateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _acg.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            return await _acg.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _acg.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _acg.UpdateAsync(data);
        }

        public async Task<int> UpdateArticleCateGoryAsync(int id, string title)
        {
            var data = await _acg.GetById(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.UpdateTime = DateTime.Now;
            return await _acg.UpdateAsync(data);
        }
    }
}
